package com.cpd.blog.controller;

import com.cpd.blog.common.annotation.LogAnnotation;
import com.cpd.blog.dto.Result;
import com.cpd.blog.dto.User.LoginReq;
import com.cpd.blog.dto.User.RegisterReq;
import com.cpd.blog.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 登录接口
 *
 * @author Copydown
 * @date 2021/8/16 19:57
 */
@Api(tags = "注册登陆模块")
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final LoginService loginService;

    @ApiOperation(value = "登陆 接口")
    @PostMapping("login")
    @LogAnnotation(module = "api", operator = "登陆", record = false)
    public Result login(@RequestBody LoginReq loginReq) {
        return loginService.login(loginReq);
    }

    @ApiOperation(value = "退出登陆 接口")
    @GetMapping("logout")
    public Result logout(@RequestHeader("apiToken") String token) {
        return loginService.logout(token);
    }

    @ApiOperation(value = "注册 接口")
    @PostMapping("register")
    @LogAnnotation(module = "api", operator = "注册", record = false)
    public Result register(@Validated @RequestBody RegisterReq registerReq) {
        return loginService.register(registerReq);
    }
}
